package com.zzl.finance.data.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.data.dto.AddressBookAddDto;
import com.zzl.finance.data.dto.AddressBookUpdateDto;
import com.zzl.finance.data.entity.AddressBookEntity;
import com.zzl.finance.data.service.AddressBookService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Map;

/**
 * @author sunpp
 * @date 2021-11-29 13:17:46
 */
@ApidocController("3通讯录管理")
@RestController
@SuppressWarnings("javadoc")
@RequestMapping("data/addressook")
@Slf4j
public class AddressBookController extends WebBaseCtrl {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApidocOperation(description = "分页查询数据", operationId = "addressBook1/q8")
    @ApidocReturnPage(genericTypeClass = AddressBookEntity.class)
    public Result list(@ApidocParamsPage(name = "params", keyName = {"userName","userEnterprise"}, keyDescription = {"用户名称","所属公司"})
                       @RequestParam Map<String, Object> params) {
        params.put("enterpriseId", this.getUser().getEnterpriseId());
        PageUtils page = addressBookService.queryPage(params);
        return Result.ok().put("page", page);
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    @ApidocOperation(description = "保存", operationId = "addressBook2")
    @ApidocReturnR
    public Result save(@ApidocParam(name = "保存的对象值", required = true) @RequestBody @Validated AddressBookAddDto addressBook) {
        try {
            addressBookService.saveAddressBook(addressBook, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 通讯录用户的 公司下拉数据
     */
    @GetMapping("/pillDownValue")
    @ApidocOperation(description = "模糊搜索公司下拉框数据", operationId = "addressBook3")
    @ApidocReturnR
    public Result pillDownValue() {
        return Result.ok().put("list", addressBookService.pullDownValue(this.getUser()));
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApidocOperation(description = "修改当前行数据", operationId = "addressBook4")
    @ApidocReturnR
    public Result update(@ApidocParam(name = "修改的对象值", required = true) @RequestBody AddressBookUpdateDto addressOok) {
        try {
            addressBookService.updateAddressOokById(addressOok, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 设置/取消 置顶
     */
    @GetMapping("/top")
    @ApidocOperation(description = "设置/取消 置顶", operationId = "addressBookTop5")
    @ApidocReturnR
    public Result top(@RequestParam("id") String id) {
        try {
            return Result.ok().message(addressBookService.isTop(id, this.getUser()));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @ApidocOperation(description = "删除数据联系人", operationId = "addressookDelete")
    @ApidocReturnR
    public Result delete(@ApidocParam(name = "删除的对象id", required = true) @PathVariable("id") Integer id) {
        addressBookService.removeByIds(Collections.singletonList(id));
        return Result.ok();
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApidocOperation(description = "通过id获取数据", operationId = "addressBook7")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "addressOok", valueClass = AddressBookEntity.class)
    })
    public Result info(@ApidocParam(name = "id", required = true) @PathVariable("id") Integer id) {
        AddressBookEntity addressOok = addressBookService.getById(id);
        return Result.ok().put("data", addressOok);
    }

}
